package library.and.xps.com.step.util;


import android.text.TextUtils;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.jude.utils.JUtils;

import org.json.JSONException;

import library.and.xps.com.step.App;
import library.and.xps.com.step.SPConstant;

public class LocationUtil {
    private static LocationClient mLocationClient;

    public static void InitLocation() {
        mLocationClient = new LocationClient(App.getInstance().getApplicationContext());
        mLocationClient.registerLocationListener(new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation bdLocation) {

                if(bdLocation!=null&&! TextUtils.isEmpty(bdLocation.getCity())){
                    JUtils.getSharedPreference().edit().putString(SPConstant.USER_SP_CITY,bdLocation.getCity()).commit();
                }

            }

            @Override
            public void onConnectHotSpotMessage(String s, int i) {

            }
        });

        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setScanSpan(1000*5);
        option.setIsNeedAddress(true);
        mLocationClient.setLocOption(option);
        mLocationClient.start();
    }

    public static void cancelLocation() {
        if (mLocationClient != null) {
            mLocationClient.stop();
        }
    }

    public interface onLocalResponse {
        void onSuccess(BDLocation bdLocation) throws JSONException;

        void onFaild(String faild);
    }
}
